package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.IOutboundCollectingRecordService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.material.OutboundCollectingRecordModel;

/**
 * @author 陈昊
 * @apiNote 原材领用控制层
 * @since 2023年06月19日 10时12分16秒
 */
@Api(value = "原材领用接口", tags = "管理--操作")
@RequestMapping(value = "/api/outboundCollectingRecord")
@RestController
public class OutboundCollectingRecordController extends ControllerBase {

    @Autowired
    IOutboundCollectingRecordService service;

    @ApiOperation(value = "查询", notes = "查询")
    @PostMapping(value = "")
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return fail("查询失败,查询条件异常!");
        }
    }

    @ApiOperation(value = "查询", notes = "分页查询")
    @PostMapping(value = "/{rowNum}/{page}")
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询", notes = "根据编号查询")
    @GetMapping("/{sn}")
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存", notes = "查询/更新信息")
    @PutMapping("")
    public WebResult saveOutboundCollectingRecord(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveOutboundCollectingRecord(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }
    @ApiOperation(value = "批量保存", notes = "批量保存/批量保存")
    @PutMapping("/savepl")
    public WebResult saveOutboundCollectingRecordPL(@ApiParam(value = "提交信息") @RequestBody OutboundCollectingRecordModel data) {
        Integer result;
        try {
            result = service.saveOutboundCollectingRecordPL(data);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }
    @ApiOperation(value = "删除信息", notes = "根据编号删除系信息")
    @DeleteMapping("")
    public WebResult delete(@ApiParam(value = "编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "删除信息", notes = "根据编号删除系信息")
    @DeleteMapping("/{sn}")
    public WebResult delete(@PathVariable("sn") String sn) {
        String[] sns = {sn};
        if (service.delete(sns) > 0) {

            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
}